//
const merge = require("lodash.merge");
const { ServiceServe, ServiceBuild, ServiceClean } = require("./service");

const { genWebpackExternals } = require("./utils");

// 插件入口
module.exports = (api, projectOptions) => {
  // default config
  const defaultPluginOpts = require('./utils/defaultConfig')
  // cnf
  let _config = merge(
    {},
    defaultPluginOpts,
    projectOptions.pluginOptions && projectOptions.pluginOptions.electronEnvOpts
  );

  // 通过 webpack-chain 修改 webpack 配置
  api.chainWebpack(webpackConfig => {
    // 修改 electron webpack配置
    if (process.env.IS_ELECTRON) {
      webpackConfig
        .target("electron-renderer")
        .externals(genWebpackExternals(api, _config));
    }
  });

  // vue-cli-service electron:serve
  api.registerCommand("electron:serve", new ServiceServe(api, _config));
  // vue-cli-service electron:build
  api.registerCommand("electron:build", new ServiceBuild(api, _config, () => {
    if(projectOptions.publicPath == '/') {
      projectOptions.publicPath = '';
    }
  }));
  // vue-cli-service electron:clean
  api.registerCommand("electron:clean", new ServiceClean(api, _config));
};

// 默认模式
module.exports.defaultModes = {
  "electron:serve": "development",
  "electron:build": "production"
};
